
import 'dart:convert';

import 'package:flutter_ahj_app/app/HttpRequest.dart';
import 'package:flutter_ahj_app/app/NewMine/mine_info.dart';
import 'package:flutter_ahj_app/app/mine/http/HttpRenZhengRequest.dart';
import 'package:flutter_ahj_app/app/my/Beans/users_operation_bean.dart';
import 'package:rxdart/rxdart.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../base/basewidget/model/BaseModel.dart';

class MyModel extends BaseModel {
  BehaviorSubject<MineInfo> _dataObservable;
  BehaviorSubject<String> _errorbservable;
  Stream<String> get errorStream => _errorbservable.stream;
  BehaviorSubject<Map> _dataMessageObservable;
  Stream<Map> get getMessageStream => _dataMessageObservable.stream;
  Stream<MineInfo> get dataStream => _dataObservable.stream;

  BehaviorSubject<Map> _loginOutSubject = BehaviorSubject();
  Stream<Map> get loginOutDataStream => _loginOutSubject.stream;

  /*
* */
  getData() {
    _dataObservable = BehaviorSubject();
    _errorbservable = BehaviorSubject();

    HttpRequest.userInfo().listen((request) {
      MineInfo userInfoBeanEntity = MineInfo.fromJson(request);

      if(!_dataObservable.isClosed){
        return _dataObservable.add(userInfoBeanEntity);
      }


    }, onError: (e) {
      //请求异常等
      if(!_dataObservable.isClosed){
        _dataObservable.add(null);
      }
      if(!_errorbservable.isClosed){
        _errorbservable.add(e.toString());
      }
      if(!errorData.isClosed){
        errorData.add(e);
      }

    });
  }

/*
* 获取未读消息数量
* */
  getMessageData(String role) {
    _dataMessageObservable = BehaviorSubject();
    HttpRenZhengRequest.cooperationNewsCount("2").listen((event) {
      if(!_dataMessageObservable.isClosed){
        return _dataMessageObservable.add(event["data"]);
      }

    }, onError: (e) {
      //请求异常等


      if(!_errorbservable.isClosed){
        _errorbservable.add(e.toString());
      }

      if(!errorData.isClosed){
        errorData.add(e);
      }
    });

  }

  BehaviorSubject<Map> _versionSubject;

  Stream<Map> get versionDataStream => _versionSubject.stream;

  checkVersion() async {
    _versionSubject = BehaviorSubject();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    // var _CONTEXT = {
    //   'ClientType': "3",
    //   'SerialNumber': "",
    //   'imis': "",
    //   'Version': packageInfo.buildNumber,
    // };
    // var _DATA = {
    //   'build': packageInfo.buildNumber,
    //   'bundleID': packageInfo.packageName,
    //   'clientType': "3",
    // };
    // String sd = json.encode(_DATA);
    // var data = {
    //   '_CONTEXT': json.encode(_CONTEXT),
    //   '_DATA': sd,
    //   '_METHOD': "AppVersion.check",
    // };
    var _DATA = {
      'version': packageInfo.version,
    };
    HttpRequest.checkVersion(_DATA).listen((request) {
      //接口请求成功 （request为map） 自己设置解析数据

      _versionSubject.add(request);
    }, onError: (e) {
      //请求异常等
      _versionSubject.add(null);

    });
  }

  

  /// 获取当前所属运营公司

  BehaviorSubject<UsersOperationBean> _usersOperationSubject;

  Stream<UsersOperationBean> get usersOperationStream =>
      _usersOperationSubject.stream;

  usersOperation() {
    _usersOperationSubject = BehaviorSubject();
    HttpRequest.usersOperation().listen((result) {
      print(result);
      UsersOperationBean bean = UsersOperationBean.fromJson(result);

      if(!_usersOperationSubject.isClosed){
        _usersOperationSubject.add(bean);
      }
    }, onError: (e) {
      if(!errorData.isClosed){
        errorData.add(e);
      }
    });
  }


  loginOut() async {
    _loginOutSubject = BehaviorSubject();
    HttpRequest.userlogOut().listen((request) {
      //接口请求成功 （request为map） 自己设置解析数据
      _loginOutSubject.add(request);
    }, onError: (e) {
      //请求异常等
      errorData.add(e);
    });
  }



  // 我的钱包-是否注册
  BehaviorSubject<Map> _isRegisterDataStreamType;
  Stream<Map> get isRegisterDataStreamType =>
      _isRegisterDataStreamType.stream;

  isRegister() {
    // _isRegisterDataStreamType = new BehaviorSubject();
    // VipRequest.isRegister().listen((request) {
    //   //接口请求成功 （request为map） 自己设置解析数据
    //   _isRegisterDataStreamType.add(request);
    // }, onError: (e) {
    //   //请求异常等
    //   _isRegisterDataStreamType.add(null);
    //   errorData.add(e);
    // });
  }

  // 我的钱包-获取token
  BehaviorSubject<Map> _getTokenDataStreamType;
  Stream<Map> get getTokenDataStreamType =>
      _getTokenDataStreamType.stream;

  getToken() {
    // _getTokenDataStreamType = new BehaviorSubject();
    // VipRequest.getToken().listen((request) {
    //   //接口请求成功 （request为map） 自己设置解析数据
    //   _getTokenDataStreamType.add(request);
    // }, onError: (e) {
    //   //请求异常等
    //   _getTokenDataStreamType.add(null);
    //   errorData.add(e);
    // });
  }

  ///是否存在专家认证记录
  BehaviorSubject<Map> _isExistCer;
  Stream<Map> get getIsExistCerStream => _isExistCer.stream;

  getIsExistCer() {
    _isExistCer = BehaviorSubject();
    HttpRequest.getIsExistCer().listen((event) {
      _isExistCer.add(event);
      print(event);
    }, onError: (e) {
      errorData.add(e);
    });
  }

  void dispose() {
    if (_dataObservable != null) {
      _dataObservable.close();
    }
    if (_loginOutSubject != null) {
      _loginOutSubject.close();
    }
    if(_errorbservable != null) {
      _errorbservable.close();
    }
    if (_versionSubject != null) _versionSubject.close();
    if (_usersOperationSubject != null) {
      _usersOperationSubject.close();
    }
    if (_isRegisterDataStreamType != null) {
      _isRegisterDataStreamType.close();
    }
    if (_getTokenDataStreamType != null) {
      _getTokenDataStreamType.close();
    }
    if (_isExistCer != null) {
      _isExistCer.close();
    }
    super.dispose();
  }
}
